<?php
/**
 * LoginController.php hanterar in och utdata
 * Tar emot indata från vyer.
 * Ser till att rätt ändringar sker i model
 * Använder rätt vyer för att skapa utdata
 * Hanterar Inloggning/Utloggning av användare
 */
class LoginController {
	private $m_loginHandler;
	private $m_memberView;
	private $m_loginView;

	public function __construct($a_loginHandler, $a_memberView, $a_loginView) {
		$this -> m_loginHandler = $a_loginHandler;
		$this -> m_memberView = $a_memberView;
		$this -> m_loginView = $a_loginView;
	}

	function DoControl() {
		$xhtml = "";
		/*
		 * Did user try to login? Good then we'll get the input username and password and check it against our database
		 * Everything ok? Then tell user it did good and set cookies if it wanted you to do that. Did the user do bad? Show ERROR MESSAGE!!!  
		 */
		if ($this->m_loginView -> TriedToLogin()) {
			if ($this -> m_loginHandler -> DoLogin($this->m_loginView -> GetUserName(), $this->m_loginView -> GetPassword())) {
				$xhtml .= $this->m_loginView->LoginSuccess();
				if ($this->m_loginView -> IsCheckedBox()) {
					$this->m_loginView -> SetCookieNSetLogoutState();
				}
			} else {
				$xhtml .= $this->m_loginView -> ErrorMessageLog();
			}
			//If the user didn't try to log in, we should make it, with a loginform. 
		}else{
			$xhtml .= $this->m_loginView -> DoLoginForm();
		}
		return $xhtml;
	}
	
	//Make a ugly login-/logutbutton (depending on we're logged in or not) 
	public function GetLoginOutButton (){
		return $this->m_loginView->GetLoginOutButton($this->m_loginHandler->IsLoggedIn());	
	}
}
